package org.reliablesource.jdt.viewers;

import org.eclipse.jdt.core.ICompilationUnit;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jdt.core.IMethod;
import org.eclipse.jdt.core.IPackageFragment;
import org.eclipse.jdt.core.JavaModelException;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.SelectionChangedEvent;
import org.reliablesource.client.ui.viewers.ReliableElementContentProvider;

/**
 *
 *  ContributorContentProvider.java<br>
 *  Copyright (c) 2007, Reliable Source, Inc. All Rights Reserved<br><br>
 *
 *	Created on: May 16, 2007 <br><br>
 *  @author Dennis Park 
 *<a href="mailto:dennis.park@gmail.com">dennis.park@gmail.com</a>
 *
 * @since 1.0.0
 */
public class ContributorContentProvider extends ReliableElementContentProvider {

	public void selectionChanged(SelectionChangedEvent event) {
		currentSelection = (IStructuredSelection) event.getSelection();
	}

	/**
	 * Returns the elements in the input, which must be either an array or a
	 * <code>Collection</code>.
	 */
	public Object[] getElements(Object _element) {
		if (_element instanceof IJavaProject){
			try {
				return ((IJavaProject) _element).getPackageFragments();
			} catch (JavaModelException e) {}
		}else if(_element instanceof IPackageFragment){
			try {
				return ((IPackageFragment) _element).getCompilationUnits();
			} catch (JavaModelException e) {}
		}else if(_element instanceof ICompilationUnit){
			try {
				return ((ICompilationUnit) _element).getChildren();
			} catch (JavaModelException e) {}
		}		
		return super.getElements(_element);
	}

	public Object[] getChildren(Object _parentElement) {
		if (_parentElement instanceof IJavaProject){
			try {
				return ((IJavaProject) _parentElement).getChildren();
			} catch (JavaModelException e) {
				e.printStackTrace();
			}
		}
		else if(_parentElement instanceof IPackageFragment){
			try {
				return ((IPackageFragment) _parentElement).getCompilationUnits();
			} catch (JavaModelException e) {}
		}
		else if(_parentElement instanceof ICompilationUnit){
			try {
				return ((ICompilationUnit) _parentElement).getChildren();
			} catch (JavaModelException e) {}
		}		
			return super.getChildren(_parentElement);
	}

	public Object getParent(Object element) {
		if (element instanceof IPackageFragment)
			return ((IPackageFragment) element).getParent();
		if (element instanceof ICompilationUnit)
			return ((ICompilationUnit) element).getParent();				
		if (element instanceof IMethod)
				return ((IMethod) element).getParent();			
		return super.getParent(element);
	}

	public boolean hasChildren(Object element) {
		if (element instanceof IJavaProject)
			try {
				return ((IJavaProject) element).getChildren() != null && ((IJavaProject) element).getChildren().length > 0;
			} catch (JavaModelException e) {
			}
			else if(element instanceof IPackageFragment){
				try {
					return ((IPackageFragment) element).getCompilationUnits() != null && ((IPackageFragment) element).getCompilationUnits().length > 0;
				} catch (JavaModelException e) {}
			}
			else if(element instanceof ICompilationUnit){
				try {
					return ((ICompilationUnit) element).getChildren() != null && ((ICompilationUnit) element).getChildren().length > 0;
				} catch (JavaModelException e) {}
			}
		return super.hasChildren(element);
	}
}
